//
//  TMItemPageController.m
//  国学培训用户端
//
//  Created by Tao Li on 2018/7/11.
//  Copyright © 2018年 mac. All rights reserved.
//

#import "TMItemPageController.h"
#import "TMItemDetailController.h"
#import "VTMagic.h"
#import "TMRecommendModel.h"
@interface TMItemPageController ()<VTMagicViewDataSource,VTMagicViewDelegate>
@property (nonatomic, strong) NSMutableArray *itemWidths;
@property (nonatomic, strong) VTMagicController *magicController;
@property (nonatomic, strong) NSMutableArray *nameArray;
@end

@implementation TMItemPageController
- (VTMagicController *)magicController
{
    if (!_magicController) {
        _magicController = [[VTMagicController alloc] init];
   _magicController.view.translatesAutoresizingMaskIntoConstraints = YES;
        _magicController.magicView.navigationColor = [UIColor whiteColor];//导航栏的颜色
        
        _magicController.magicView.switchStyle = VTSwitchStyleDefault;
        _magicController.magicView.layoutStyle = VTLayoutStyleDefault;
        _magicController.magicView.headerHidden = NO;
        _magicController.magicView.againstStatusBar = NO;
        _magicController.magicView.sliderExtension = 10.0;
        _magicController.magicView.sliderColor = [UIColor hx_colorWithHexRGBAString:@"#05B48D"];
        _magicController.magicView.dataSource = self;
        _magicController.magicView.delegate = self;
        _magicController.magicView.itemScale = 1.1;//点中的item变大
    }
    return _magicController;
}
-(NSMutableArray *)nameArray{
    if (!_nameArray) {
        _nameArray = [NSMutableArray array];
    }
    return _nameArray;
}
- (void)viewWillAppear:(BOOL)animated{
    [super viewWillAppear:animated];
   
}
- (void)viewDidLoad
{
    [super viewDidLoad];
    self.navigationItem.title = @"培训";
    for (TMRecommendModel *model in self.lists) {
        [self.nameArray addObject:model.name];
    }
    self.edgesForExtendedLayout = UIRectEdgeAll;
    [self addChildViewController:self.magicController];
    [self.view addSubview:_magicController.view];
    [self.view setNeedsUpdateConstraints];
    [_magicController.magicView reloadData];
    [_magicController.magicView reloadDataToPage:self.selectIndex];
}
- (NSArray<__kindof NSString *> *)menuTitlesForMagicView:(VTMagicView *)magicView{
    return self.nameArray;
}

- (UIButton *)magicView:(VTMagicView *)magicView menuItemAtIndex:(NSUInteger)itemIndex
{
    static NSString *itemIdentifier = @"itemIdentifier";
    UIButton *menuItem = [magicView dequeueReusableItemWithIdentifier:itemIdentifier];
    if (!menuItem) {
        menuItem = [UIButton buttonWithType:UIButtonTypeCustom];
        [menuItem setTitleColor:[UIColor hx_colorWithHexRGBAString:@"#3B3B3B"] forState:UIControlStateNormal];
        [menuItem setTitleColor:[UIColor hx_colorWithHexRGBAString:@"#05B48D"] forState:UIControlStateSelected];
        menuItem.titleLabel.font = [UIFont fontWithName:@"Helvetica" size:16.f];
    }
    return menuItem;
}

- (UIViewController *)magicView:(VTMagicView *)magicView viewControllerAtPage:(NSUInteger)pageIndex{
    static NSString *gridId = @"grid.identifier";
    TMItemDetailController *gridViewController = [magicView dequeueReusablePageWithIdentifier:gridId];
    if (!gridViewController) {
        gridViewController = [[UIStoryboard storyboardWithName:@"Home" bundle:nil] instantiateViewControllerWithIdentifier:@"TMItemDetailController"];
    }
    TMRecommendModel *model = self.lists[pageIndex];
    gridViewController.type = model.cid;
    return gridViewController;
}

@end
